package com.axt.manager.bean;

import com.axt.soa.bean.*;
import com.axt.soa.msg.types.MsgParam;
import com.axt.manager.base.*;
import com.axt.manager.model.*;

import java.util.*;

public class NodeBean extends BaseFuncBean {
	// 增加节点 返回增加的节点ID
	public int addNode(String serverName, String serverType, String serverAddr, String port) {
		prepare();
		request.addParam(MsgParam.PARA_SVR_NAME, serverName);
		request.addParam(MsgParam.PARA_SVR_TYPE, serverType);
		request.addParam(MsgParam.PARA_ADDRESS, serverAddr);
		request.addParam(MsgParam.PARA_PORT, port);

		if(-1 == execute(MsgFuncExt.FUNC_ADD_NODE, CONN_T_TCP)){
			return -1;
		}
		return reply.getParam(MsgParam.PARA_SVR_ID).toInteger();
	}

	// 删除节点
	public int deleteNode(String serverId) {
		prepare();
		request.addParam(MsgParam.PARA_SVR_ID, serverId);

		return execute(MsgFuncExt.FUNC_DELETE_NODE, CONN_T_TCP);
	}

	// 修改节点
	public int modifyNode(
			String serverId, String serverName, String serverType, String serverAddr, String port) {
		prepare();
		request.addParam(MsgParam.PARA_SVR_ID, serverId);
		request.addParam(MsgParam.PARA_SVR_NAME, serverName);
		request.addParam(MsgParam.PARA_SVR_TYPE, serverType);
		request.addParam(MsgParam.PARA_ADDRESS, serverAddr);
		request.addParam(MsgParam.PARA_PORT, port);

		return execute(MsgFuncExt.FUNC_MODIFY_NODE, CONN_T_TCP);
	}

	// 获取NodeList
	public List<Node> queryNodeList(int userId) {
		prepare();
		request.addParam(MsgParam.PARA_USER_ID, userId);
		
		return fetch(MsgFuncExt.FUNC_QUERY_NODE, 
				"com.axt.manager.model.Node", MsgParam.PARA_SVR_LIST, CONN_T_TCP);
	}
	
	// 获取ServerId
	public int queryServerId(String serverAddr, String port) {
		prepare();
		request.addParam(MsgParam.PARA_ADDRESS, serverAddr);
		request.addParam(MsgParam.PARA_PORT, port);

		if (-1 == execute(MsgFuncExt.FUNC_GET_SERVER_ID, CONN_T_TCP)) {
			return -1;
		}

		return reply.getParam(MsgParam.PARA_SVR_ID).toInteger();
	}
}
